<?php
/**
 * 项目管理模块
 * User: lyc
 * Date: 2016/11/18
 * Time: 10:56
 */

class ApiInvestService extends BaseService
{
    private static $self = NULL;
    private $mItem = NULL;
    private $mEnterprise = NULL;
    private $mRiskControl = NULL;
    private $mUserAward = NULL;
    private $mInvest = NULL;
    private $mRefundItemPlan = NULL;
    private $mUser = NULL;
    private $mUserTicket = NULL;
    private $mExperItem = NULL;
    private $mAd = NULL;
    private $mInterlocution =NULL;



    static public function instance()
    {
        if (self::$self == NULL) {
            self::$self = new self;
        }
        return self::$self;
    }


    public function __construct()
    {
        $this->mItem = new Involved_ItemModel();
        $this->mEnterprise = new Involved_EnterpriseModel();
        $this->mRiskControl = new Involved_RiskControlModel();
        $this->mInvest = new Involved_InvestModel();
        $this->mUserAward = new Involved_UserAwardModel();
        $this->mRefundItemPlan = new Involved_RefundItemPlanModel();
        $this->mUser = new Involved_UserModel();
        $this->mUserTicket = new Involved_UserTicketModel();
        $this->mExperItem = new Involved_ExperItemModel();
        $this->mAd = new Involved_AdModel();
        $this->mInterlocution = new Involved_InterlocutionModel();

    }


    /**
     * 参数验证
     * @param $obj
     * @param $arr
     * @author alice
     * @date 2016-1-13
     * @return bool
     * @return bool
     */
    public function  checkProperties($obj, $arr)
    {
        if (empty($arr)) return true;
        $obj = (array)$obj;

        foreach ($arr as $v) {
            if (is_null($obj[$v])) {
                return false;
            }
        }
        return true;
    }



    /**
     * @param $page
     * @return mixed
     * 理财列表
     */
    public function FinancingList($page){

        $data =  $this->mItem->GetApiFinancing($page);
        return $data;

    }
    /**
     * @param $page
     * @return mixed
     * 理财Banner
     */
    public function FinancingBanner(){
        $data =  $this->mAd->FinancingBanner();
        return $data;

    }
    /**
     * @param $page
     * @return mixed
     * 理财详情
     */
    public function GetApiFinancingDetails($paramObj){
        return $financing = $this->mItem->GetApiFinancingDetails($paramObj);

    }
    /**
     * GetApiFinancingBalanceAmount
     * 查询余额
     */
    public function GetApiFinancingBalanceAmount($paramObj){
        return $financing = $this->mItem->GetApiFinancingDetails($paramObj);

    }


    /**
     * 投资记录
     * @param $page
     * @return mixed
     * @throws Exception
     */
    public function InvestRecord($page)
    {

        if (empty($_SESSION[UserEnum::USER_INFO_USER_ID])) {
            throw new Exception('用户未登录！', ErrorCodeEnum::STATUS_ERROR_NOT_LOGIN);
        }
        $user_id = $_SESSION[UserEnum::USER_INFO_USER_ID];

        if(empty($page)){
            $page == 1;
        }
        $data = $this->mInvest->InvestRecord($page,$user_id);
        if(!empty($data)){
            foreach($data as $k=>$v){
                if($v->status=='REPAYING'){
                    $v->status  =  '计息中';
                }elseif($v->status=='FINISHED'){
                    $v->status  =  '收益完成';
                }else{
                    $v->status  =  '投资成功';
                }
                if(empty($v->repaying_time)){
                    $v->repaying_time = '融满起息';
                }else{
                    $v->repaying_time =  $v->repaying_time +$v->deadline * 86400;
                    $v->repaying_time = date('Y-m-d', $v->repaying_time);
                }

                if($v->award==''){
                    $v->award = 0;
                }
                if($v->rate==''){
                    $v->rate = 0;
                }

                $v->award = $v->award/100;
                $v->rate  = $v->rate/100;
                $v->amount  =  $v->amount/100;

                $v->expect_earnings  =  number_format(($v->rate_loaner+$v->rate)*$v->amount*$v->deadline/365/100+$v->award,2);

            }
        }
        return $data;
    }
    /**
     * 商票
     */
    public function GetSP($paramObj){
        return $data = $this->mItem->GetSP($paramObj);
    }
    /**
     * 材料
     */
    public function GetFile($paramObj){
        return $data = $this->mItem->GetFile($paramObj);
    }
    /**
     * 资金流水
     * @param $page
     * @param $type
     * @return mixed
     * @throws Exception
     */
    public function MoneyRecord($page,$type)
    {


        if (empty($_SESSION[UserEnum::USER_INFO_USER_ID])) {
            throw new Exception('用户未登录！', ErrorCodeEnum::STATUS_ERROR_NOT_LOGIN);
        }
        $user_id = $_SESSION[UserEnum::USER_INFO_USER_ID];

        if(empty($page)){
            $page == 1;
        }
        $data = $this->mInvest->MoneyRecord($page,$type,$user_id);

        $arr = array();
        foreach($data as $k=>$v)
        {
            $arr[$k] = (json_decode($v->update_after,true));
        }
        $arr2 = array();
        foreach($arr as $k=>$v)
        {
            $arr2[$k] = $v['money_deposit']+$v['money_award']+$v['money_return'];
        }

        foreach($data as $k=>$v)
        {
            foreach($arr2 as $kk=>$vv){
                if($k==$kk){
                    $v->update_after = number_format($vv/100,2);
                }
            }
        }
        foreach($data as $k=>$v){
            if($v->type == 'DE_CASH'){
                $v->amount = number_format($v->amount/100,2);
                $v->type = '提现';
//                foreach(StatusEnum::CashStatus() as $kk=>$vv){
//                    if($v->status == $kk){
//                        $data[$k]->status = $vv;
//                    }
//                }
                if($v->status=='SUCCESS'){
                    $v->status='提现成功';
                }else{
                    $v->status='待确认';
                }
            }elseif($v->type == 'DE_INVEST'){
                $v->amount = number_format($v->amount/100,2);
                $v->type = '投资';
            }elseif($v->type == 'IN_DEPOSIT'){
                $v->amount = '+'.number_format($v->amount/100,2);
                $v->type = '充值';
            }elseif($v->type == 'IN_REFUND'){
                $v->amount = '+'.number_format($v->amount/100,2);
                $v->type = '回款';
            }elseif($v->type == 'IN_EXPER'){
                $v->amount = '+'.number_format($v->amount/100,2);
                $v->type = '体验标回款';
            }elseif($v->type == 'IN_AWARD'){
                $v->amount = '+'.number_format($v->amount/100,2);
                $v->type = '奖励';
            }elseif($v->type == 'IN_TICKET'){
                $v->amount = '+'.number_format($v->amount/100,2);
                $v->type = '加息劵';
            }elseif($v->type == 'IN_CASH_CANCEL'){
                $v->amount = '+'.number_format($v->amount/100,2);
                $v->type = '取消提现';
            }

        }
        return $data;
    }
    /**
     *
     * 投资记录
     */
    public function GetInvest($paramObj){
        return $invest= $this->mInvest->GetApiInvest($paramObj);
    }

    /**
     *获取问答
     */

    public function GetInterlocution(){
        return $invest= $this->mInterlocution->GetInterlocution();
    }
    /**
     * 获取最优红包
     */
    public function GetApiAward($paramObj){
        $array=array();
        if($paramObj->qixian < 30){
            $award=$array;
        }
        elseif($paramObj->qixian >=30 && $paramObj->qixian < 60 ){
            $award = InvestService::instance()->Thirty($paramObj);//根据天数来查可用的红包
        }
        elseif($paramObj->qixian >=60 ){
            $award = InvestService::instance()->Sixty($paramObj);//根据天数来查可用的红包
        }
        else{
            $award=$array;
        }
        return $award;
    }



    /**
     * 体验标详情
     * @param $paramObj
     * @return mixed
     * @throws Exception
     */
    public function ExperDetails($paramObj)
    {
        #参数验证
        $check = $this->checkProperties($paramObj, ['exper_id']);
        if (!$check) {
            throw new Exception($this->error_msg[ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST],
                ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST);
        }

        if (!empty($_SESSION[UserEnum::USER_INFO_USER_ID])) {
            $user_id = $_SESSION[UserEnum::USER_INFO_USER_ID];
        }

        $data = InvestService::instance()->ApiTiyan();
        $amount = InvestService::instance()->ExperAllAmount($paramObj->exper_id);                  //融资金额总数

        if(!empty($user_id)) {
            $user_info = InvestService::instance()->ExperInvest($paramObj->exper_id, $user_id);
        }

        if(!empty($user_info)){
            $data->user_info= $user_info;
            $all_amount = '';
            foreach($amount as $k=>$v){
                $all_amount +=$v->exper_amount;
            }
            $data->all_amount = $all_amount;
            return $data;
        }else if(!empty($amount)){
            $all_amount = '';
            foreach($amount as $k=>$v){
                $all_amount +=$v->exper_amount;
            }
            $data->all_amount = $all_amount;
            return $data;
        }else{
            return $data;
        }

    }




    /**
     * 体验标投资
     * @param $paramObj
     * @return mixed
     * @throws Exception
     */
    public function ExperBid($paramObj)
    {

        if (empty($_SESSION[UserEnum::USER_INFO_USER_ID])) {
            throw new Exception('用户未登录！', ErrorCodeEnum::STATUS_ERROR_NOT_LOGIN);
        }
        $user_id = $_SESSION[UserEnum::USER_INFO_USER_ID];

        #参数验证
        $check = $this->checkProperties($paramObj, ['exper_id', 'exper_amount']);
        if (!$check) {
            throw new Exception($this->error_msg[ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST],
                ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST);
        }

        $now = time();
        $begin_time = strtotime(date('Y-m-d', time()))+36000;
        if($now>$begin_time){
            $data = InvestService::instance()->ExperInvest($paramObj->exper_id,$user_id);
            $tiyan = InvestService::instance()->Tiyan();
            if($tiyan->is_valid == 1){
                if(empty($data)){
                    $data2 = InvestService::instance()->ExperInsert($paramObj->exper_id,$user_id,$paramObj->exper_amount);
                    if(!empty($data2)){
//                       return InvestService::instance()->ExperInvest($paramObj->exper_id,$user_id);
                        return new stdClass();
                    }
                }else{
                    throw new Exception("已经投资过了", ProfessionErrorCodeEnum::ERROR_USER_MOBILE_FAILURE);
                }
            }else{
                throw new Exception("已经下标", ProfessionErrorCodeEnum::ERROR_USER_MOBILE_FAILURE);
            }
        }else{
            throw new Exception("未到投标时间", ProfessionErrorCodeEnum::ERROR_USER_MOBILE_FAILURE);
        }


    }





















    /***********************************************************M端*****************************************************/
    public function FinancingListWeb(){
        $data =  $this->mItem->GetWebFinancing();
        return $data;
    }




}